<?php

namespace app\admin\model\prl;

use think\Model;


class Views extends Model
{

    

    // 表名
    protected $table = 'prl_views';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'field_ids',//字段ids
    ];
    

    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4'), '5' => __('Type 5'), '6' => __('Type 6')];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function viewsfields()
    {
        return $this->belongsToMany('Viewsfields', 'app\\admin\\model\\prl\\Viewsfieldsaccess');
    }

    public function getFieldIdsAttr($value, $data)
    {
        $ids = [];
        foreach ($this->viewsfields as $field) {
            $ids[] = $field->pivot->viewsfields_id;
        }
        return $ids;
    }

    //获取视图文件
    public function getFileList()
    {
        $config = get_addon_config('blcms');
        if(empty($config)) $config['theme'] = 'default';
        $dir = '../addons/prl/view/' . $config['theme'];
        $list = [];
        $this->read_theme_file($dir, $list);
        
        return $list;
    }

    //读取视图文件列表
    protected function read_theme_file($dir, &$list = [], $ext = '')
    {
        if(!is_dir($dir)) return false;

        $arr = scandir($dir);

        foreach ($arr as $value) {
            if($value == '.' || $value =='..')
                continue;

            if(is_dir($dir . DIRECTORY_SEPARATOR . $value)){
                $this->read_theme_file($dir . DIRECTORY_SEPARATOR . $value, $list, $ext . $value . '/');
                continue;
            }

            $list[] = $ext . $value;
        }
        return $list;
    }
}
